.. note::
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
**Why Join?**
- **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
- **Learn & Share**: Exchange tips and tutorials to enhance your skills.
- **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
- **Special Discounts**: Enjoy exclusive discounts on our newest products.
- **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!
2.1.2 Slide Switch
==================
Introduction
------------
In this lesson, we will learn how to use a slide switch. Usually,the
slide switch is soldered on PCB as a power switch, but here we need to
insert it into the breadboard, thus it may not be tightened. And we use
it on the breadboard to show its function.
Components
----------
.. image:: img/list_2.1.2_slide_switch.png
Principle
---------
**Slide Switch**
.. image:: img/image156.jpeg
A slide switch, just as its name implies, is to slide the switch bar to
connect or break the circuit, and further switch circuits. The
common-used types are SPDT, SPTT, DPDT, DPTT etc. The slide switch is
commonly used in low-voltage circuit. It has the features of flexibility
and stability, and applies in electric instruments and electric toys
widely.
How it works: Set the middle pin as the fixed one. When you pull the
slide to the left, the two pins on the left are connected; when you pull
it to the right, the two pins on the right are connected. Thus, it works
as a switch connecting or disconnecting circuits. See the figure below:
.. image:: img/image304.png
The circuit symbol of the slide switch is shown as below. The pin2 in
the figure refers to the middle pin.
.. image:: img/image159.png
**Capacitor**
The capacitor is a component that has the capacity to store energy in
the form of electrical charge or to produce a potential difference
(Static Voltage) between its plates, much like a small rechargeable
battery.
Standard Units of Capacitance
Microfarad (μF) 1μF = 1/1,000,000 = 0.000001 = :math:`10^{- 6}` F
Nanofarad (nF) 1nF = 1/1,000,000,000 = 0.000000001 = :math:`10^{- 9}`\ F
Picofarad (pF) 1pF = 1/1,000,000,000,000 = 0.000000000001 =
:math:`10^{- 12}`\ F
.. note::
Here we use **104 capacitor(10 x 10\ 4\ PF)**. Just like the
ring of resistors, the numbers on the capacitors help to read the values
once assembled onto the board. The first two digits represent the value
and the last digit of the number means the multiplier. Thus 104
represents a power of 10 x 10 to 4 (in pF) equal to 100 nF.
Schematic Diagram
-----------------
Connect the middle pin of the Slide Switch to GPIO17, and two LEDs to
pin GPIO22 and GPIO27 respectively. Then when you pull the slide, you
can see the two LEDs light up alternately.
.. image:: img/image305.png
.. image:: img/image306.png
Experimental Procedures
-----------------------
**Step 1:** Build the circuit.
.. image:: img/image161.png
:width: 800
**Step 2**: Go to the folder of the code.
.. raw:: html
.. code-block::
cd ~/davinci-kit-for-raspberry-pi/c/2.1.2
**Step 3**: Compile.
.. raw:: html
.. code-block::
gcc 2.1.2_Slider.c -lwiringPi
**Step 4**: Run the executable file above.
.. raw:: html
.. code-block::
sudo ./a.out
While the code is running, get the switch connected to the left, then
the yellow LED lights up; to the right, the red light turns on.
.. note::
If it does not work after running, or there is an error prompt: \"wiringPi.h: No such file or directory\", please refer to :ref:`install_wiringpi_pi5`.
**Code**
.. code-block:: c
#include
#include
#define slidePin 0
#define led1 3
#define led2 2
int main(void)
{
// When initialize wiring failed, print message to screen
if(wiringPiSetup() == -1){
printf("setup wiringPi failed !");
return 1;
}
pinMode(slidePin, INPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
while(1){
// slide switch high, led1 on
if(digitalRead(slidePin) == 1){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
printf("LED1 on\n");
delay(100);
}
// slide switch low, led2 on
if(digitalRead(slidePin) == 0){
digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
printf(".....LED2 on\n");
delay(100);
}
}
return 0;
}
**Code Explanation**
.. code-block:: c
if(digitalRead(slidePin) == 1){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
printf("LED1 on\n");
}
When the slide is pulled to the right, the middle pin and right one are
connected; the Raspberry Pi reads a high level at the middle pin, so the
LED1 is on and LED2 off
.. code-block:: c
if(digitalRead(slidePin) == 0){
digitalWrite(led2, LOW);
digitalWrite(led1, HIGH);
printf(".....LED2 on\n");
}
When the slide is pulled to the left, the middle pin and left one are
connected; the Raspberry Pi reads a low, so the LED2 is on and LED1 off